package Demo.菜鸟驿站;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Register extends JFrame {

    Staff[] staff = new Staff[50];

    public void Register() {

        staff[0] = new Staff(1, "傅粟海", "1", "1", "员工1", "138");
        staff[1] = new Staff(2, "王保淞", "wangbaosong", "123456", "管理员", "156");
        staff[2] = new Staff(3, "向德才", "xiangdecai", "123456", "员工", "137");
        staff[3] = new Staff(4, "小鸡超", "2", "2", "员工", "133");

        JTextField ID;
        JPasswordField Password;
        JPanel p1, p2, p3;
        JLabel id, password;
        JButton jbutton1, jbutton2;
        //设置窗口标题
        this.setTitle("登录窗口");
        //设置位置和尺寸
        this.setBounds(300, 100, 300, 200);
        //关闭窗口
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(new GridLayout(3, 1, 5, 5));
        ID = new JTextField(20);
        id = new JLabel("账号:");
        Password = new JPasswordField(20);
        password = new JLabel("密码：");
        jbutton1 = new JButton("确认");
        jbutton2 = new JButton("取消");
        p1 = new JPanel();
        p2 = new JPanel();
        p3 = new JPanel();
        p1.add(id, BorderLayout.WEST);
        p1.add(ID, BorderLayout.EAST);
        p2.add(password, BorderLayout.WEST);
        p2.add(Password, BorderLayout.EAST);
        this.add(p1, BorderLayout.NORTH);
        this.add(p2, BorderLayout.CENTER);
        p3.add(jbutton1, BorderLayout.WEST);
        p3.add(jbutton2, BorderLayout.EAST);
        this.add(p3, BorderLayout.SOUTH);
        this.setVisible(true);

        jbutton1.addActionListener(new ActionListener() {
            int num = 0;

            public void actionPerformed(ActionEvent e) {
                for (int i = 0; i < staff.length; i++) {
                    String username = ID.getText();
                    String password = new String(Password.getPassword());
                    if (staff[i].account.equals(username) && (new String(staff[i].password)).equals(password)) {
                        JOptionPane.showMessageDialog(null, "登陆成功");

                        if (staff[i].post.equals("管理员")) {
                            Administrator a = new Administrator();
                            a.administrator();
                            break;
                        }else if (staff[i].post.equals("员工")) {
                            Job b = new Job();

                            break;
                    }

                    } else {
                        JOptionPane.showMessageDialog(null, "密码错误");
                        ID.setText("");
                        Password.setText("");
                        num++;
                        break;
                    }
                }
                if (num == 2) {
                    JOptionPane.showMessageDialog(null, "您还有最后一次机会");

                }
                if (num == 3) {
                    JOptionPane.showMessageDialog(null, "密码错误达到三次关闭程序");
                    System.exit(1);
                }
            }
        });
        Staff[] g = new Staff[4];
        for (int i = 0; i < staff.length; i++) {
            if (staff[i] != null) {
                g[i] = staff[i];
            } else {
                break;
            }

        }
    }

    public class Administrator extends JFrame {
        private JLabel j1;
        private JPanel p1, p2;
        private JTextField txtInput;
        private JButton jbutton;
        int a;

        public void administrator() {
            this.setTitle("管理员界面");
            //设置位置和尺寸
            this.setBounds(300, 100, 300, 200);
            //关闭窗口
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setLayout(new GridLayout(6, 1, 5, 5));
            j1 = new JLabel("欢迎使用员工管理系统\n" + "1.查看所有员工\n" + "2.添加员工\n" + "3.炒他鱿鱼\n" + "4.修改密码\n" + "5.安全退出\n");
            p1 = new JPanel();
            p1.add(j1);
            jbutton = new JButton("确认");
            txtInput = new JTextField(20);
            p2 = new JPanel();
            p2.add(txtInput, BorderLayout.WEST);
            p2.add(jbutton, BorderLayout.EAST);
            this.add(p1);
            this.add(p2, BorderLayout.SOUTH);
            jbutton.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    a = Integer.parseInt(txtInput.getText());
                    switch (a) {
                        case 1:
                            lista lista = new lista();
                            lista.listaa();
                            break;
                        case 2:
                            add add = new add();
                            add.add();
                            break;
                        case 3:
                            delete delete = new delete();
                            delete.delete();
                            break;
                        case 4:
                            modification modification = new modification();
                            modification.modification();
                            break;
                        case 5:
                            System.exit(0);
                            break;
                    }
                }
            });
            this.setVisible(true);
        }
    }

    public class lista extends JFrame {
        public void listaa() {
            JLabel a;
            //设置窗口标题
            this.setTitle("员工管理窗口");
            //设置位置和尺寸
            this.setBounds(300, 100, 300, 600);
            //关闭窗口
            this.setLayout(new GridLayout(10, 1, 10, 10));

            for (int i = 0; i < staff.length; i++) {
                if (staff[i] != null) {
                    a = new JLabel(staff[i].name + "职位是：" + staff[i].post);
                    this.add(a);
                } else {
                    break;
                }
            }
            this.setVisible(true);
        }
    }

    public class add extends JFrame {
        private void add() {
            JTextField txtInput;
            JButton jbutton;
            JPanel p1, p2;
            //设置窗口标题
            this.setTitle("员工管理窗口");
            //设置位置和尺寸
            this.setBounds(300, 100, 300, 600);
            //关闭窗口
            this.setLayout(new GridLayout(10, 1, 10, 10));
            JOptionPane.showMessageDialog(null, "请输入名字");
            jbutton = new JButton("确认");
            p1 = new JPanel();
            txtInput = new JTextField(20);
            p1.add(txtInput, BorderLayout.WEST);
            p1.add(jbutton, BorderLayout.EAST);
            this.add(p1, BorderLayout.SOUTH);
            jbutton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String name = txtInput.getText();

                    int index = -1;
                    for (int i = 0; i < staff.length; i++) {
                        if (staff[i] == null) {
                            index = i;
                            break;
                        }
                    }
                    if (index != -1) {
                        staff[index] = new Staff();
                        staff[index].num = index + 1;
                        staff[index].password = "123456";
                        staff[index].name = name;
                        staff[index].post = "员工";
                        JOptionPane.showMessageDialog(null, "添加成功");
                    } else {
                        JOptionPane.showMessageDialog(null, "员工已满");
                    }
                }
            });
            this.setVisible(true);
        }

    }

    public class delete extends JFrame {
        boolean flag = false;

        private void delete() {
            JTextField txtInput;
            JButton jbutton;
            JPanel p1;

            //设置窗口标题
            this.setTitle("员工开除窗口");
            //设置位置和尺寸
            this.setBounds(300, 100, 300, 600);
            //关闭窗口
            this.setLayout(new GridLayout(10, 1, 10, 10));
            JOptionPane.showMessageDialog(null, "请输入需要删除的员工的名字");
            jbutton = new JButton("确认");
            p1 = new JPanel();
            txtInput = new JTextField(20);
            p1.add(txtInput, BorderLayout.WEST);
            p1.add(jbutton, BorderLayout.EAST);
            this.add(p1, BorderLayout.SOUTH);
            jbutton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String name = txtInput.getText();

                    int index = -1;
                    for (int i = 0; i < staff.length; i++) {
                        if (staff[i] != null) {
                            if (staff[i].name.equals(name)) {
                                index = i;


                                while (staff[index + 1] != null) {
                                    staff[index].num = staff[index + 1].num;
                                    staff[index].name = staff[index + 1].name;
                                    staff[index].account = staff[index + 1].account;
                                    staff[index].password = staff[index + 1].password;
                                    staff[index].post = staff[index + 1].post;
                                    staff[index].phone = staff[index + 1].phone;
                                    index++;
                                }
                                staff[index] = null;
                                flag = true;
                            }
                        }
                    }
                    if (flag) {
                        JOptionPane.showMessageDialog(null, "删除成功");
                    } else {
                        JOptionPane.showMessageDialog(null, "删除失败");
                    }
                }

            });
            this.setVisible(true);
        }


    }


    public class modification extends JFrame {
        boolean flag = false;
        private void modification() {
            JTextField txtInput,Name;
            JPasswordField Password,Password1;
            JButton jbutton;
            JPanel p0,p1, p2, p3;
            JLabel a1, a2, a3,a4;
            //设置窗口标题
            this.setTitle("修改员工密码");
            //设置位置和尺寸
            this.setBounds(300, 100, 300, 600);
            //关闭窗口
            this.setLayout(new GridLayout(5, 1, 10, 10));

            txtInput = new JTextField(20);
            Password = new JPasswordField(20);
            Password1 = new JPasswordField(20);
            Name= new JTextField(30);
            jbutton = new JButton("确认");
            a1 = new JLabel("请输入旧密码");
            a2 = new JLabel("请输入新密码");
            a3 = new JLabel("确认新密码");
            a4 = new JLabel("账号");
            p0 = new JPanel();
            p1 = new JPanel();
            p2 = new JPanel();
            p3 = new JPanel();
            p0.add(a4,BorderLayout.NORTH);
            p0.add(Name,BorderLayout.SOUTH);


            p1.add(a1, BorderLayout.NORTH);
            p1.add(txtInput, BorderLayout.SOUTH);

            p2.add(a2, BorderLayout.NORTH);
            p2.add(Password, BorderLayout.SOUTH);

            p3.add(a3, BorderLayout.NORTH);
            p3.add(Password1, BorderLayout.SOUTH);
            this.add(p0,BorderLayout.NORTH);
            this.add(p1,BorderLayout.NORTH);
            this.add(p2,BorderLayout.CENTER);
            this.add(p3,BorderLayout.SOUTH);
            this.add(jbutton,BorderLayout.SOUTH);

            jbutton.addActionListener(new ActionListener(){

                @Override
                public void actionPerformed(ActionEvent e) {
                    for (int i =0;i<staff.length; i++){
                        if (staff[i]!=null){
                            if (staff[i].account.equals(Name.getText())&&staff[i].password.equals(txtInput.getText())&&(new String(Password.getPassword())).equals (new String(Password1.getPassword()))){
                                staff[i].password = String.valueOf(Password.getText());
                                JOptionPane.showMessageDialog(null,"修改成功");
                                flag=true;
                                break;
                            }
                        }else {
                            flag=false;
                        }
                    }
                    if (flag) {
                        JOptionPane.showMessageDialog(null, "修改成功");
                    } else {
                        JOptionPane.showMessageDialog(null, "信息错误，修改失败");
                    }
                }

            });

            this.setVisible(true);

        }
    }
}